
VERSION_PATTERN = '\d+.\d+.\d+(.\d+){0,1}'

def compare_tag(a, b)
	v1 = a.match(VERSION_PATTERN)
	v2 = b.match(VERSION_PATTERN)
	if (v1 != nil and v2 != nil) then
		i1 = v1.to_s.split('.')
		i2 = v2.to_s.split('.')
		if i1.count == i2.count then
			if a == b then
				return 0
			else
				i = 0
				while i < i1.count
					if i1[i].to_i > i2[i].to_i then
						return 1
					elsif i1[i].to_i < i2[i].to_i
						return -1
					end
					i = i + 1
				end
				return -1
			end
		else
			return a <=> b
		end
	else
		return a <=> b
	end
end

def list_tags(tags_url)
	cmd = "svn list #{tags_url}"
	tags = `#{cmd}`
	if $? == 0
		sorted = tags.split().sort { |a, b| compare_tag(a, b) }
		sorted.each do |tag|
			puts tags_url + tag
		end
	end
end

def create_new_tag(trunk_url, tags_url, newtag)
	if newtag != nil then
		if tags_url.end_with?("/")
			target = "#{tags_url + newtag}"
		else
			target = "#{tags_url + "/" + newtag}"
		end
		puts "Tag to #{target}"
		cmd = "svn copy #{trunk_url} #{target} -m \"New tag #{newtag} created.\""
		ret = `#{cmd}`
		if $? == 0 then
			puts "Tag #{newtag} created successfully."
			return
		else
			puts ret
		end
	end
end

URL = {
	:VBS => [
		"https://sami.cdt.int.thomsonreuters.com/svn/add_vap/trunk/VAA/VAPScripting/tags/",
		"https://sami.cdt.int.thomsonreuters.com/svn/add_vap/trunk/VAA/VAPScripting/trunk/"
	],
	:IST => [
		"https://sami.cdt.int.thomsonreuters.com/svn/add_awg/trunk/IST/Core/tags/",
		"https://sami.cdt.int.thomsonreuters.com/svn/add_awg/trunk/IST/Core/trunk/"
	],
	:WG => [
		"https://sami.cdt.int.thomsonreuters.com/svn/add_awg/trunk/Core/trunk/WebGrabber/tags/",
		"https://sami.cdt.int.thomsonreuters.com/svn/add_awg/trunk/Core/trunk/WebGrabber/trunk/"
	],
	:PPDS => [
		"https://sami.cdt.int.thomsonreuters.com/svn/ppds_ppdssource/tags/",
		"https://sami.cdt.int.thomsonreuters.com/svn/ppds_ppdssource/trunk/IDN.Net35/"
	],
	:PPDSPUB => [
		"https://sami.cdt.int.thomsonreuters.com/svn/ppds_ppdssource/tags/PPDSPublisher/",
		"https://sami.cdt.int.thomsonreuters.com/svn/ppds_ppdssource/trunk/IDN.Net35/"
	]
}

def help()
	puts "vaptag - Create or list tags from SVN"
	puts "usage: vaptag COMMAND target [label]"
	puts "COMMAND:"
	puts "    info          - show the URL of tag and trunk"
	puts "    list          - list all tags under the URL"
	puts "    tag           - create a new tag"
	puts "target:"
	puts "    vbs           - VAP VBScripts"
	puts "    ist           - IST"
	puts "    wg            - Web Grabber"
	puts "    ppds          - PPDS"
	puts "    ppds-pub      - PPDS Publisher"
	puts "label:"
	puts "    new label for tag command"
end

def main()
	idx_url = 0
	tags_url = ""
	trnk_url = ""
	
	if ARGV.size < 2
		help()
		exit
	end
	
	case ARGV[1].upcase
		when "WG"
			tags_url = URL[:WG][0]
			trnk_url = URL[:WG][1]
		when "IST"
			tags_url = URL[:IST][0]
			trnk_url = URL[:IST][1]
		when "VBS"
			tags_url = URL[:VBS][0]
			trnk_url = URL[:VBS][1]
		when "PPDS"
			tags_url = URL[:PPDS][0]
			trnk_url = URL[:PPDS][1]
		when "PPDS-PUB"
			tags_url = URL[:PPDSPUB][0]
			trnk_url = URL[:PPDSPUB][1]
		else
			puts "Invalid target."
			exit
	end
	
	case ARGV[0]
		when "info"
			puts " TAGS URL: " + tags_url
			puts "TRUNK URL: " + trnk_url
		when "list"
			list_tags(tags_url)
		when "tag"
			if ARGV.size != 3
				help()
				exit
			end
			newtag = ARGV[2]
			create_new_tag(trnk_url, tags_url, newtag)
		else
			puts "Invalid arguments."
	end
end

main
